package com.hskn.hss.module.instorageCfg.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.instorageCfg.entity.TlInStorageCfg;
import com.hskn.hss.module.instorageCfg.mapper.TlInStorageCfgMapper;
import com.hskn.hss.module.instorageCfg.service.ITlInStorageCfgService;
import com.hskn.hss.module.planDetail.entity.TlPlanDetail;
import com.hskn.hss.module.planDetail.service.ITlPlanDetailService;
import com.hskn.hss.module.procurementPlan.entity.TlProcurementPlan;
import com.hskn.hss.module.procurementPlan.mapper.TlProcurementPlanMapper;
import com.hskn.hss.module.storageDetail.service.ITlStorageDetailService;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-08
 */
@Service
public class TlInStorageCfgServiceImpl extends ServiceImpl<TlInStorageCfgMapper, TlInStorageCfg> implements ITlInStorageCfgService {

    @Resource
    TlInStorageCfgMapper tlInStorageCfgMapper;
    @Autowired
    ITlPlanDetailService tlPlanDetailService;
    @Resource
    TlProcurementPlanMapper tlProcurementPlanMapper;
    @Autowired
    ITlStorageDetailService tlStorageDetailService;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class}) //添加事务
    public AjaxResult insert(JSONObject params) {
        TlInStorageCfg TlInStorageCfg = params.getObject("data", TlInStorageCfg.class);
        int i = tlInStorageCfgMapper.insert(TlInStorageCfg);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class}) //添加事务
    public AjaxResult update(JSONObject params) {
        try {
            String uid = LoginUtils.getUser().getUid();
            String id = params.getString("id");
            TlInStorageCfg tlInStorageCfg = tlInStorageCfgMapper.selectById(id);
            tlInStorageCfg.setStatus("1");
            tlInStorageCfg.setIndealperson(uid);
            tlInStorageCfg.setIndealtime(new Date());
            TlProcurementPlan tlProcurementPlan = tlProcurementPlanMapper.selectById(tlInStorageCfg.getPlanid());
            tlProcurementPlan.setStatus("4");
            tlProcurementPlanMapper.updateById(tlProcurementPlan);//回填 计划表状态
            List<TlPlanDetail> TlPlanDetailList = tlPlanDetailService.getitemListByParentId(tlInStorageCfg.getPlanid());
            /**
             * 计入库存，完成入库开始
             */
            String msg = tlStorageDetailService.updateadd(tlInStorageCfg.getStoragecenterid(), "add", TlPlanDetailList);
            /**
             * 计入库存，完成入库结束
             */
            int i = tlInStorageCfgMapper.updateById(tlInStorageCfg);

            return AjaxResult.success(msg);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败！请稍后再试");
        }


    }


    @Override
    public TlInStorageCfg getDetailById(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlInStorageCfg TlInStorageCfg = tlInStorageCfgMapper.selectById(id);
        List<TlPlanDetail> TlPlanDetailList = tlPlanDetailService.getitemListByParentId(TlInStorageCfg.getPlanid());
        if (!TlPlanDetailList.isEmpty()) {
            TlInStorageCfg.setDateils(TlPlanDetailList);
        }
        return TlInStorageCfg;
    }

    @Override
    public JSONObject getList(JSONObject params) {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map map = new HashMap();
        map.put("serialnumber", params.getString("serialnumber"));
        map.put("storagecenterid", params.getString("storagecenterid"));
        map.put("indealperson", params.getString("indealperson"));
        map.put("starttime", params.getString("starttime"));
        map.put("endtime", params.getString("endtime"));
        map.put("status", params.getString("status"));


        Page<TlInStorageCfg> inStorageCfgPage = new Page<>(page, size, true);
        IPage<TlInStorageCfg> iPage = tlInStorageCfgMapper.listinstorage(inStorageCfgPage, map);

        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("instoragelist", iPage.getRecords());
        return json;
    }
}
